
import javax.xml.stream.*;

import java.io.*;
import java.util.*;

public class Object2XML {

    public static <K,V> void mapToXml(Map<K, V> map, Writer out) throws IOException, XMLStreamException {
        XMLStreamWriter xsw = null;
        try {
            try {
                XMLOutputFactory xof = XMLOutputFactory.newInstance();
                // If you want pretty-printing, you can use:
                //xsw = new javanet.staxutils.IndentingXMLStreamWriter(xof.createXMLStreamWriter(out));
                xsw = xof.createXMLStreamWriter(out);
                xsw.writeStartDocument("utf-8", "1.0");
                xsw.writeStartElement("entries");

                // Do the Collection
                for(Map.Entry<K, V> e : map.entrySet()) {
                    xsw.writeStartElement("entry");
                    xsw.writeAttribute("key", e.getKey().toString());
                    xsw.writeAttribute("value", e.getValue().toString());
                    xsw.writeEndElement();
                }
                xsw.writeEndElement();
                xsw.writeEndDocument();
            }
            finally {
                if (out != null) {
                    try { out.close() ; } catch(IOException e) { /* ignore */ }      
                }
            }// end inner finally
        }
        finally {
            if (xsw != null) {
                try { xsw.close() ; } catch(XMLStreamException e) { /* ignore */ }    
            }
        }
    }
    
    public static <K,V> void listToXml(List<Map<K, V>> mapList, Writer out) throws IOException, XMLStreamException {
        XMLStreamWriter xsw = null;
        try {
            try {
                XMLOutputFactory xof = XMLOutputFactory.newInstance();
                // If you want pretty-printing, you can use:
                //xsw = new javanet.staxutils.IndentingXMLStreamWriter(xof.createXMLStreamWriter(out));
                xsw = xof.createXMLStreamWriter(out);
                xsw.writeStartDocument("utf-8", "1.0");
                xsw.writeStartElement("table");
                System.out.println(mapList);
                for(Map<K, V> map:mapList){
                	 xsw.writeStartElement("record");
                	 for(Map.Entry<K, V> e : map.entrySet()) {
                		 xsw.writeStartElement("column");
                         xsw.writeAttribute("name", e.getKey().toString());
                         if (e.getValue() == null)
                         {
                        	 xsw.writeAttribute("value", "");
                        	 }
                        	 else
                        	 {
                        		 xsw.writeAttribute("value", e.getValue().toString());
                        	 }
                         
                         xsw.writeEndElement();
                     }
                	 xsw.writeEndElement();
                }
                xsw.writeEndElement();
                xsw.writeEndDocument();
            }
            finally {
                if (out != null) {
                    try { out.close() ; } catch(IOException e) { /* ignore */ }      
                }
            }// end inner finally
        }
        finally {
            if (xsw != null) {
                try { xsw.close() ; } catch(XMLStreamException e) { /* ignore */ }    
            }
        }
    }



}
